package com.ljk.study8httpfunction.httputil;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 * getForObject  和 getForEntity  的区别:
 * getForObject函数实际上是对getForEntity函数的进一步封装，如果你只关注返回的消息体的内容，对其他信息都不关注，此时可以使用getForObject。
 * ResponseEntity<T>是Spring对HTTP请求响应的封装，包括了几个重要的元素，如响应码、contentType、contentLength、响应消息体等。如果需要提取其中的部分属性的话直接从ResponseEntity中调用相应的方法即可。
 * </p>
 *
 * @author ljk
 * @date 2021/3/29 23:00
 */
public class RestTemplateUtil {
    private static RestTemplate restTemplate = new RestTemplateBuilder()
            .setConnectTimeout(Duration.ofMillis(1000))
            .setReadTimeout(Duration.ofMillis(1000))
            .build();

    public static void main(String[] args) {
        String url = "http://127.0.0.1:8080/hello";

//        getFunction1(url);
//        getFunction2(url);
        postFunction1(url);
//        postFunction2(url);

    }

    /**
     * POST请求:设置自定义header的方式
     *
     * @param url
     */
    private static void postFunction1(String url) {
        HttpHeaders httpHeaders = new HttpHeaders();
        //header增加个属性
        httpHeaders.add("yourName", "ljk");
        // 设置请求参数流格式
        httpHeaders.setContentType(MediaType.valueOf("application/json; charset=utf-8"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("ljk");
        map.put("name", list);
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(map, httpHeaders);
//        Map<String, String> map = new HashMap<>();
//        map.put("name","ljk");
//        HttpEntity<Map> requestEntity = new HttpEntity<Map>(map, httpHeaders);
        String result = restTemplate.postForObject(url, requestEntity, String.class);
        System.out.println(result);
    }

    /**
     * POST请求方法: 默认header
     *
     * @param url
     */
    private static void postFunction2(String url) {
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("ljk");
        map.put("name", list);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
        System.out.println(responseEntity.getBody());
    }


    /**
     * get请求的方式:在地址上用占位符,用map的方式装填参数
     *
     * @param url
     */
    private static void getFunction2(String url) {
        url += "?name={name}";
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "ljk");
//        String body = restTemplate.getForObject(url, String.class, map);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, map);
        System.out.println(forEntity.getBody());
    }

    /**
     * get请求的方式:在地址上用占位符,在方法中用可变数组填参数
     *
     * @param url
     */
    private static void getFunction1(String url) {
        url += "?name={1}";
        ResponseEntity<String> ljk = restTemplate.getForEntity(url, String.class, "ljk");
//        String ljk1 = restTemplate.getForObject(url, String.class, "ljk");
        System.out.println(ljk.getStatusCode());
    }


}
